<template>
	<view style="background-color: #C3B18B; box-sizing: border-box; padding-left: 20rpx; padding-right: 20rpx;">
		<view class="storage_header">
			<!-- 登录 -->
			<view class="header-left" v-if="hasLogin">
				<text class="log">{{userInfo.nickName}}</text>
			</view>
			<view class="header-left" v-else @click="getUserProfile">
				<text class="log">请登录</text>
				<text>Please Log In</text>
			</view>
			<!-- 扫一扫 -->
			<view class="header-right">
				<view class="iln-box">
					<text class="icon iconfont icon-saoyisao"></text>
				</view>
				<view class="iln-box" @click="toUser">
					<text class="icon1 iconfont icon-zcpt-weixincaidanguanli"></text>
				</view>
			</view>
			<!-- 下拉按钮 -->
			<view class="box" @click="tomy">
				<text class="flexd iconfont icon-shuangxiala"></text>
			</view>
		</view>
	</view>
</template>

<script>
	import BaseUrl from "../../utlis/api.js"
	export default {
		props: ["giveUrlKey", "shareUrlKey", "boxId"],
		data() {
			return {
				userInfo: null,
				hasLogin: false,
				rows: null
			}
		},
		onLoad() {

		},
		created() {
			this.getLogin();

		},
		methods: {
			toUser() {
				uni.navigateTo({
					url: '/pages/user/user'
				})
			},
			getLogin() {
				if (wx.getStorageSync("userInfo")) {
					this.hasLogin = true
					const userInfo = wx.getStorageSync("userInfo")
					this.userInfo = userInfo
				}
			},
			tomy() {
				uni.navigateTo({
					url: '/pages/index/index',
					success() {
						let page = getCurrentPages().pop(); //跳转页面成功之后
						if (!page) return;
						page.onLoad(); //如果页面存在，则重新刷新页面
					}
				})
			},
			getUserProfile(e) {
				wx.login({
					success(res) {
						wx.setStorageSync("code", res.code)
					}
				})
				wx.getUserProfile({
					desc: '用于完善会员资料',
					success: (res) => {
						console.log(res)
						const userInfo = res.userInfo;
						this.userInfo = res.userInfo
						this.hasLogin = true;
						console.log("用户同意授权信息")
						const code = wx.getStorageSync("code")
						wx.setStorageSync("userInfo", userInfo);
						const data = {
							userInfo: userInfo,
							code: code
						}
						uni.request({
							url: BaseUrl + "/app/login/LoginByMa",
							method: 'POST',
							data,
							success: (res) => {
								console.log(res)
								if (res.data.code == 200) {
									const token = res.data.msg;
									wx.setStorageSync("token", token);
									wx.setStorageSync("userId", res.data.data.userId);
									if (this.shareUrlKey) {
										let that = this
										wx.showModal({
											title: '共享您一个盒子',
											content: '是否接受',
											success: function(res) {
												if (res.confirm) {
													const boxId = that.boxId
													const key = that.shareUrlKey
													console.log(boxId, key)
													const token = wx.getStorageSync('token') || [];
													uni.request({
														url: BaseUrl + 'wx/sbox/box/share?boxId=' + boxId + '&key=' + key,
														method: 'POST',
														header: {
															"Authorization": "Bearer " + token,
														},
														success: (res) => {
															console.log(res)
															this.getDataList()
														}
													})
												} else {
													console.log('用户点击取消')
												}
											}
										})
									}
									if (this.giveUrlKey) {
										let that = this
										wx.showModal({
											title: '赠送您一个盒子',
											content: '是否接受',
											success: function(res) {
												if (res.confirm) {
													const boxId = that.boxId
													const key = that.giveUrlKey
													console.log(boxId, key)
													const token = wx.getStorageSync('token') || [];
													uni.request({
														url: BaseUrl + 'wx/sbox/box/give?boxId=' + boxId + '&key=' + key,
														method: 'POST',
														header: {
															"Authorization": "Bearer " + token,
														},
														success: (res) => {
															console.log(res)
															this.getDataList()
														}
													})
												} else {
													console.log('用户点击取消')
												}
											}
										})
									}

								} else {
									console.log("登录失败")
								}
							}
						})
					}
				})
			},
			getDataList() {
				if (wx.getStorageSync("userInfo")) {
					const token = wx.getStorageSync('token') || [];
					const userId = wx.getStorageSync('userId');
					uni.request({
						url: BaseUrl + "wx/sbox/box/list?userId=" + userId,
						method: 'GET',
						header: {
							"Authorization": "Bearer " + token,
						},
						data: userId,
						success: (res) => {
							const msg = res.data.msg
							this.rows = res.data.rows
							this.$emit("getChild", this.rows);
							uni.showToast({
								title: msg
							})
						}
					})
				}
			},
		}
	}
</script>

<style>
	.storage_box {
		width: 100%;
		height: 100%;
		font-size: 28rpx;
	}

	.storage_header {
		display: flex;
		justify-content: space-between;
		position: relative;
	}

	.header-left,
	.header-right {
		width: 50%;
		color: #eee;
		font-size: 32rpx;
	}

	.log {
		display: inline-block;
		width: 100%;
		font-size: 48rpx;
	}

	.iln-box {
		display: inline-block;
		box-sizing: border-box;
		padding-top: 30rpx;
		padding-left: 40rpx;
	}

	.header-right {
		text-align: right;
	}

	.icon {
		font-size: 40rpx;
	}

	.icon1 {
		font-size: 48rpx;
	}

	.flexd {
		font-size: 48rpx;
		color: #eee;
		position: absolute;
		left: 50%;
		bottom: 0;
	}
</style>
